﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Entity;
using BotonJXC.Util;
using NHibernate.Criterion;

namespace BotonJXC.DAO
{
    public class AllocatesDAO : NHibernateDao<Allocates, int>
    {
        /// <summary>
        /// 条件查询调拨单
        /// </summary>
        /// <param name="allocates"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public PageModel<Allocates> findAllocatesByCondition(Allocates allocates, int start, int count)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (allocates.CreateTime.HasValue)
            {
                criterions.Add(Restrictions.Ge("CreateTime", allocates.CreateTime.Value));
            }
            if (allocates.EndCreateTime.HasValue)
            {
                criterions.Add(Restrictions.Le("CreateTime", allocates.EndCreateTime.Value));
            }
            if (allocates != null)
            {
                if (Utils.IsNotBlank(allocates.AllocateSn))
                {
                    criterions.Add(Restrictions.Like("AllocateSn", allocates.AllocateSn, MatchMode.Anywhere));
                }
                if (allocates.AuditStatus > 0)
                {
                    criterions.Add(Restrictions.Eq("AuditStatus", allocates.AuditStatus));
                }
                if (allocates.InDepartment!=null )
                {
                    criterions.Add(Restrictions.Eq("InDepartment.DepId", allocates.InDepartment.DepId));
                }
                if (allocates.OutDepartment != null)
                {
                    criterions.Add(Restrictions.Eq("OutDepartment.DepId", allocates.OutDepartment.DepId));
                }
                if (allocates.AllocateType > 0) 
                {
                    criterions.Add(Restrictions.Eq("AllocateType", allocates.AllocateType));
                }
                if (allocates.OutWarehouse != null)
                {
                    criterions.Add(Restrictions.Eq("OutWarehouse.WarehouseId", allocates.OutWarehouse.WarehouseId));
                }
                if (allocates.InWarehouse != null)
                {
                    criterions.Add(Restrictions.Eq("InWarehouse.WarehouseId", allocates.InWarehouse.WarehouseId));
                }
            }
            return base.FindByPage(criterions.ToArray(), Order.Desc("AllocateId"), start, count);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderSn"></param>
        /// <returns></returns>
        public IList<Allocates> findAllocatesBySn(string AllocateSn,string AllocateType)
        {
            List<ICriterion> criterions = new List<ICriterion>();
            if (Utils.IsNotBlank(AllocateSn))
            {
                criterions.Add(Restrictions.Eq("AllocateSn", AllocateSn));
            }
            if (Utils.IsNotBlank(AllocateType))
            {
                criterions.Add(Restrictions.Eq("AllocateType", AllocateType));
            }
            return base.Find(criterions.ToArray());
        }

    }
}
